/*
 * GccApplication1.c
 *
 * Created: 4/5/2012 11:21:41 AM
 *  Author: Brad
 */ 
#define CLEARED = 0;

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdlib.h>
#include <stdio.h>

//prototypes
void print(int val);
void print(char str[], int chars);
void init(void);

int rawInputData = 0; //try volatile if problems

int main(void)
{
	_delay_ms(5000);	// waits for sensor to initialize
	init();				




	int i;
	int humidity=0;
	int temperature=0;
	while(1){
		_delay_ms(2000);
		DDRB = 0x01;		// sets PortB.0 direction to output
		PORTB = 0x00;		// sets PortB.0 output to low
		_delay_ms(5);
		
//		PORTB  = 0x01;
//		for(i=0; i<45; i++);

		DDRB = 0x00;		// sets PortB.0 direction to input
		PORTB = 0x01;		// sets PortB.0 internal pull-up to Enabled
	
		while((PINB & _BV(PINB0))); // while sensor == high
		while(!(PINB & _BV(PINB0)));// while sensor == low
		while((PINB & _BV(PINB0))); // while sensor == high
									
		// sensor now begins to transmit
		
		for(i=0; i < 40; i++)
		{
			if(i == 16)
			{
				humidity = rawInputData;
				rawInputData = CLEARED;
			}
			if(i == 32)
			{
				temperature = rawInputData;
				rawInputData = CLEARED;
			}

			while(!(PINB & _BV(PINB0)));		// while sensor == low
						
			// when sensor = high						
			TCCR0A = 0x01;						// init timer
			rawInputData = (rawInputData << 1);	// shifts input data left 1
			while((PINB & _BV(PINB0)));			// while sensor == high
			
			if(TCNT0 > 100)						// if timer_count > value
				rawInputData |= 0x01;			// store 1 (most significant bits first)	
	
			TCCR0A = 0x00;			// disabled timer
			TCNT0 = CLEARED;		// resets timer_count
		}


/*
		char buf[10];
		itoa(temperature, buf, 10);
		//sprintf(buf, "%x", temperature);
		for(i = 0; i < 5; i++)
		{
		while(!(UCSR0A & _BV(UDRE0)));
		UDR0 = buf[i];
		}
 */

// removing temporarily
//------
/*
		int temp_bits = temperature;
		for(i=0; i<32; i++)
		{
			while(!(UCSR0A & _BV(UDRE0)));
			if(temp_bits & 0x01)
				UDR0 = '1';
			else
				UDR0 = '0'; 
			
			temp_bits = temp_bits >> 1;
		}

		while(!(UCSR0A & _BV(UDRE0)));
		UDR0 = ' ';

		temp_bits = humidity;
		for(i=0; i<32; i++)
		{
			while(!(UCSR0A & _BV(UDRE0)));
			if(temp_bits & 0x01)
				UDR0 = '1';
			else
				UDR0 = '0'; 
			
			temp_bits = temp_bits >> 1;
		}

		while(!(UCSR0A & _BV(UDRE0)));
		UDR0 = ' ';
*/
//-----------
/*		while(!(UCSR0A & _BV(UDRE0)));
		if(temperature == 0)			
			UDR0 = 'Y';
		else 
			UDR0 = 'N';

		while(!(UCSR0A & _BV(UDRE0)));
		if(humidity == 0)			
			UDR0 = 'Y';
		else 
			UDR0 = 'N';
 */

	}

	return 1;
}




void init(void)
{
	DDRB = 0x01;		// sets PortB.0 direction to Output
	PORTB = 0x01;		// sets PortB.0 output to High
	UCSR0A = 0x02;      // U2x=1
	UCSR0B |= 0x12;		// TX and RX are enabled for now
    UCSR0C =  0x06;     // no parity, stop 1 bit, 8data bits

	

	UBRR0H = 0x00;		// baud rate high register
	UBRR0L = 0x0c;		// sets baud rate to 19200 on 2mHz frequency
	sei();				// activate global interrupts
	EIMSK = 0xc0;	//we actually probably don't need this since we're not using interrupts anymore
					// set the pin change interrupt for PORTB PIN1
}



void print(int val, int base)
{
	char buf[10];
	itoa(val, buf, base);
	
	int i=0;
	while(buf[i] != '\0')
	{
		while(!(UCSR0A & _BV(UDRE0)));
		UDR0 = buf[i];
	}		
}
void print(char str[], int chars)
{
	
	
	
}

